package com;

import com.fz.entity.Book;
import com.fz.entity.Tom;
import com.fz.mapper.BookMapper;
import com.fz.mapper.TomMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by webrx on 2017/6/13.
 */
public class Tsst {
    protected SqlSessionFactory sf;
    protected SqlSession ss;
    @Before
    public void init(){
        InputStream inputStream =null;
        try {
            inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        sf = new SqlSessionFactoryBuilder().build(inputStream);
        ss = sf.openSession();
    }
    @Test
    public void show(){
        //接口的插入
    /* TomMapper tom =ss.getMapper(TomMapper.class);
        Tom tt = new Tom();
        tt.setName("大***");
        tom.save(tt);
        //接口的删除
        tom.deleteById(3);
        ss.commit();
        //借口的查询
        List<Tom> list =tom.query();
        for(Tom ttt:list){
            System.out.println( ttt.getName());

        }*/
        BookMapper boo = ss.getMapper(BookMapper.class);


       /* Book bo = new Book();
        bo.setName("吗ff");
        bo.setPrive(99);
        boo.save(bo);
        ss.commit();*/

       //通过ID删除
        /*boo.deleteById(2);
        ss.commit();*/

        //直接添加
        /*boo.add("<海样>",902);
        ss.commit();*/

        //通过ID修改
       /* boo.update("海鸥",5);
        ss.commit();*/

        /*//通过调对象全部修改
       Book book06=boo.selectByID(4);
       book06.setName("海象");
       book06.setPrive(6);
       boo.updateAll(book06);
       ss.commit();*/
        //根据ID修改价格
       /* boo.updateBu(55,5);
        ss.commit();*/

        //根据ID价格+1
       /*boo.updateBU01(1,6);
       ss.commit();*/

        //全部价格+N
       /*boo.updateBUAll02(1);
        ss.commit();*/
       //单独查询某个对象
      /* Book book =boo.selectByID(4);
        System.out.println(book.getName());
        System.out.println(book.getPrive());
        System.out.println(book.getId());*/

        //模糊查询
       /* List<Book> books = boo.queryname("%好%");
        for(Book bb :books){
            System.out.println(bb.getName());
        }
        System.out.println("**********");
*/

        /*Map<String,Object> map = new HashMap<String, Object>();
        map.put("name","数学");
        map.put("price",999);
        boo.addbook(map);
        ss.commit();*/

        /*int currpage =2;
        int pagesize=2;
        List<Book>  books1=boo.page(currpage*pagesize-pagesize,pagesize);
        for(Book BB :books1){
            System.out.println(BB.getName());
        }*/

       List<Map<String,Object>> list022= boo.queryNamePrice();
       for(Map<String,Object> mm: list022){
           System.out.println(mm.get("name"));
           System.out.println(mm.get("prive"));
           System.out.println("____");

       }

       /* List<Book> kk = boo.query();
        for(Book rr :kk){
            System.out.println(rr.getName());

        }*/


       /*List<Book> li = ss.selectList("com.fz.mapper.BookMapper.queryA");
       for(Book bb :li){
           System.out.println(bb.getName()+"+++++");
       }*/

      /*List<Tom> list = ss.selectList("com.fz.mapper.TomMapper.queryAll");
       for(Tom t :list){
           System.out.println(t.getName()+"***");
       }*/
    }
}
